<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>友链修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <div class="layui-form layuimini-form">
        <input type="hidden" name="id" th:value="${friend.id}">
        <div class="layui-form-item">
            <label class="layui-form-label required">友链名</label>
            <div class="layui-input-block">
                <input type="text" name="name" th:value="${friend.name}" lay-verify="required" lay-reqtext="友链名不能为空" placeholder="请输入友链名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="1" title="男" th:checked="${friend.sex}">
                <input type="radio" name="sex" value="0" title="女" th:checked="${friend.sex == false}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">友链接</label>
            <div class="layui-input-block">
                <input type="text" name="link" th:value="${friend.link}" placeholder="请输入友链接" lay-verify="required" lay-reqtext="友链接不能为空" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">头像地址</label>
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="selectImg">选择图片</button>
                <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
                <input id="cover" type="hidden" name="cover" lay-verify="required" lay-reqtext="请选择好头像然后点击上传图片" class="layui-input">
                <div class="layui-upload-list">
                    <img th:src="${friend.cover}" style="width: 92px; height: 120px; margin-left: 12.5%" id="pic">
                </div>
                <div style="width: 95px; margin-left: 12.5%">
                    <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="progress">
                        <div class="layui-progress-bar" lay-percent=""></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
            </div>
        </div>
    </div>

<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            element = layui.element,
            upload = layui.upload;

        upload.render({
            elem: '#selectImg',
            url: '/admin/upload/uploadFriendCover', //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            auto: false,
            bindAction: '#uploadImg',
            choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#pic').attr('src', result); //图片链接（base64）
                });
            },
            done: function(res){
                //上传成功的一些操作
                if (res.code === 200) {
                    $('#cover').val(res.data.url)
                } else if (res.code === 444) {
                    $('#picText').html(''); //置空上传失败的状态

                }
            },
            //进度条
            progress: function(n, elem, e){
                element.progress('progress', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = data.field;

            $.post('/admin/friend/editFriend', data, function (res) {
                if (res.code === 200) {
                    layer.msg(res.msg, function () {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        //刷新父页面
                        window.parent.location.reload();
                    })
                } else if (res.code === 444) {
                    layer.msg(res.msg)
                }
            })

            return false;
        });

    });
</script>
</body>
</html>
